<template>
  <div class="search">
    <div class="top">
      <span class="fh"><van-icon name="arrow-left" @click="fh" /></span>
      <van-search
        v-model="value"
        placeholder="请输入搜索关键词"
        class="kuang"
        shape="round"
        show-action
      >
        <template #action>
          <div @click="onSearch" class="ser">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="mainCenter">
      <div class="hotSearch">
        <span>
          最近搜索
          <van-icon name="delete-o" @click="delet" />
        </span>
        <ul>
          <li v-for="(i, index) in recently" :key="index" @click="zuijinss(i)">
            {{ i }}
          </li>
        </ul>
      </div>
      <div class="hotSearch">
        <span>热门搜索</span>
        <ul>
          <li v-for="i in hot" :key="i.wordid" @click="rmss(i.keyword)">
            {{ i.keyword }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "SearchIndex",
  methods: {
    fh() {
      this.$router.go(-1);
    },
    onSearch(val) {
      console.log(this.value);
      this.recently.unshift(this.value);
      localStorage.setItem("search", JSON.stringify(this.recently));
      let goodsSearch = this.value;
      if (goodsSearch == "") {
        goodsSearch = " ";
      }
      this.$router.push("SearchList/" + goodsSearch);
    },
    delet() {
      localStorage.removeItem("search");
      this.recently = [];
    },
    zuijinss(i) {
      let goodsSearch = i;
      console.log('goodsSearch: ', goodsSearch);
      this.$router.push("/SearchList/" + goodsSearch);
    },
    rmss(i) {
      let goodsSearch = i;
      this.$router.push("/SearchList/" + goodsSearch);
    },
  },
  data() {
    return {
      value: "",
      zjvalue: "",
      hot: [],
      recently: JSON.parse(localStorage.getItem("search")) || [],
    };
  },
  created() {
    this.$service({
      url: "api/pro/hotword",
    }).then((res) => {
      // console.log(res,"res");
      this.hot = res.data.data;
      // console.log(" this.hot: ", this.hot);
    });
    // this.recently=localStorage.getItem('search')
    // this.setSearch=this.recently.split(",")
    // console.log('this.recently: ', this.setSearch);
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}
.search {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.mainCenter {
  flex: 1;
  overflow: auto;
}
.top {
  text-align: center;
  line-height: 3rem;
  position: relative;
  height: 3rem;
}
.fh {
  position: absolute;
  font-size: 20px;
  left: 1rem;
}
.kuang {
  position: absolute;
  right: 0;
  margin-right: 4rem;
  margin-top: 0.5rem;
  width: 16rem;
  box-sizing: border-box;
}
.ser {
  position: absolute;
  right: -50px;
  top: 3px;
  background-color: red;
  color: white;
  font-size: 10px;
  height: 1.3rem;
  line-height: 1.3rem;
  width: 1.8rem;
}
.hotSearch {
  margin-top: 1rem;
}
.hotSearch ul {
  display: flex;
  flex-wrap: wrap;
}
.hotSearch ul li {
  width: 44px;
  flex-shrink: 0;
  border: 1px solid gray;
  font-size: 12px;
  text-align: center;
  margin-left: 10px;
  margin-top: 5px;
}
</style>